package com.canteen.create.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author bentley
 */
public class StringUtil {

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }

    /**
     * 拼接字符串
     * @param s
     * @param appendStr
     * @param split
     * @return
     */
    public static String StringAdd(String s, String appendStr, String split) {
        if (isNotEmpty(appendStr)) {
            if ("".equals(s)) {
                s = appendStr;
            } else {
                if (isNotEmpty(split)) {
                    s = s + split;
                }
                s = s + appendStr;
            }
        }
        return s;
    }

    /**
     * 拼接字符串
     * @param s
     * @param appendStr
     * @return
     */
    public static String StringAdd(String s, String appendStr) {
        return StringAdd(s, appendStr, ",");
    }

    /**
     * 下划线转小驼峰
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        char UNDERLINE = '_';

        if (param == null || isEmpty(param.trim())) {
            return "";
        }

        int len = param.length();

        //不包含下划线的，需要检查并转化为小写
        if (!param.contains("_")){
            boolean isUpper = false;
            boolean isLow = false;
            for (int i = 0; i < len; i++) {
                char c = param.charAt(i);
                if (Character.isLowerCase(c)){
                    isLow = true; //检查到小写
                }else
                if (Character.isUpperCase(c)){
                    isUpper = true; //检查到大写
                }else if (Character.isDigit(c)) {
                    isLow = true; //检查到数字就是小写
                }
            }

            //全部是大写的，需要转化为小写
            if (isUpper&&!isLow){
                return param.toLowerCase();
            }else {
                //有大写，有小写，直接返回原值
                return param;
            }
        }

        StringBuilder sb = new StringBuilder(len);
        boolean flag = false; // "_" 后转大写标志,默认字符前面没有"_"
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                flag = true;
                continue;   //标志设置为true,跳过
            } else {
                if (flag) {
                    //表示当前字符前面是"_" ,当前字符转大写
                    sb.append(Character.toUpperCase(param.charAt(i)));
                    flag = false;  //重置标识
                } else {
                    sb.append(Character.toLowerCase(param.charAt(i)));
                }
            }
        }
        return sb.toString();
    }

    /**
     * 转化为大驼峰
     * @param str
     * @return
     */
    public static String camelToUpper(String str){
        if (isEmpty(str)){
            return "";
        }

        String first = str.substring(0, 1).toUpperCase();
        return first + str.substring(1, str.length());
    }

    /**
     * 加单引号
     * @param str
     * @return
     */
    public static String addQuotes(String str){
        String result = "";
        if (!str.contains(",")){
            result = "'" + str + "'";
        }else {
            String[] strings = str.split(",");
            for (String string : strings) {
                result = StringAdd(result, "'"+string+"'", ", ");
            }
        }
        return result;
    }

    /**
     * 字符串转成List
     * @param str
     * @return
     */
    public static List<String> strToList(String str){
        List<String> list = new ArrayList<>();
        if (isEmpty(str)){
            return list;
        }

        if (!str.contains(",")){
            list.add(str);
        }else {
            String[] strings = str.split(",");
            list.addAll(Arrays.asList(strings));
        }
        return list;
    }


    /**
     * SQL对应JAVA的参数类型
     * @param sqlParam
     * @return
     */
    public static String formatSqlParam(String sqlParam){
        String paramater = "";
        switch (sqlParam){
            case "int":
            case "smallint":
                paramater = "Integer";
                break;
            case "bigint":
            case "timestamp":
                paramater = "Long";
                break;
            case "varchar":
            case "text":
            case "longblob":
                paramater = "String";
                break;
            case "datetime":
            case "date":
                paramater = "Date";
                break;
            case "decimal":
            case "double":
                paramater = "Double";
        }
        return paramater;
    }

    public static void main(String[] args) {
        System.out.println(camelToUpper("userEnd"));
    }
}
